package com.mediatek.demo.smartconnection;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Created by Maroonpu on 2018/1/23.
 */
public class MainActivity extends AppCompatActivity {

    private Button mStartButton;
    private EditText mNameEdit;
    private EditText mPswEdit;
    private TextView mWatcher;
    private Context mContext;
    private JniLoader loader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStartButton = (Button)this.findViewById(R.id.btn_start);
        mNameEdit = (EditText)this.findViewById(R.id.et_name);
        mPswEdit = (EditText)this.findViewById(R.id.et_psw);
        mWatcher = (TextView)this.findViewById(R.id.watcher);

        mContext = this;
        mStartButton.setEnabled(true);

        boolean res = JniLoader.LoadLib();
        Log.d("SmartConnection", "Load Smart Connection Library Result " + res);

        loader = new JniLoader();

        mNameEdit.addTextChangedListener(new EditChangeHandler(1));
        mPswEdit.addTextChangedListener(new EditChangeHandler(2));

        WifiManager wManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wManager.getConnectionInfo();
//判断是否连接路由器 待加
//        //限制用户修改wifi名
//        mNameEdit.setEnabled(false);
//
////        提示连接路由器
//        if(info.getSSID().equals("<unknown ssid>")){
//            showWarningDialog(R.string.warning_link_router);
//            return ;
//        }
////        System.out.println(info.getSSID());
////        System.out.println(info.getSSID().length());
        if (info.getSSID().isEmpty() == false) {
            String name = info.getSSID();
            String ssid = name.substring(1, name.length() - 1);
            mNameEdit.setText(ssid);
            mNameEdit.setSelection(mNameEdit.getText().length());
            Log.d("SmartConnection", "SSID " + mNameEdit.getText().toString());
        }

        //限制用户修改wifi名
        mNameEdit.setEnabled(false);
    }

    @Override
    protected void onStart() {
        super.onStart();

        mStartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                int retValue = JniLoader.ERROR_CODE_OK;

                retValue = loader.InitSmartConnection("", null, 0, 1, 0);
                if (retValue != JniLoader.ERROR_CODE_OK) {
                    showWarningDialog(R.string.init_failed);
                    Log.e("SmartConnection", "Init failed : " + retValue);
                    return;
                }
//                暂定密码不能为空
                if(mPswEdit.getText().toString().isEmpty() == true){
                    showWarningDialog(R.string.warning_input_password);
                    return;
                }

                String SSID = mNameEdit.getText().toString();
                String Password = mPswEdit.getText().toString();
                String Custom = "rcvFlag";
                Log.d("elian_app", "SSID - " + SSID + ", Password - " + Password + ", Custom - " + Custom);
                retValue = loader.StartSmartConnection(SSID, Password, Custom);
                if (retValue != JniLoader.ERROR_CODE_OK) {
                    showWarningDialog(R.string.start_failed);
                    Log.e("SmartConnection", "Start failed : " + retValue);
                    return;
                }

                Intent intent = new Intent();
                intent.setClass(MainActivity.this,SettingActivity.class);
                intent.putExtra("loader",loader);
                startActivity(intent);


            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void showWarningDialog(final int stringID) {
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                AlertDialog dialog = new AlertDialog.Builder(mContext)
                        .setTitle(R.string.warning)
                        .setMessage(stringID)
                        .setPositiveButton("确定",null)
                        .create();
                dialog.show();
                dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#3bafda"));
            }

        });
    }

    private class EditChangeHandler implements TextWatcher {

        private int which;

        public EditChangeHandler(int which) {
            this.which = which;
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub
            String str = arg0.toString();
            boolean hid = false;

            switch(which) {
                case 1:
                    if (str.getBytes().length > 32) {
                        mWatcher.setText("SSID Exceed Max Length (32)");
                    } else {
                        hid = true;
                    }
                    break;
                case 2:
                    if (str.getBytes().length > 64) {
                        mWatcher.setText("Password Exceed Max Length (64)");
                    } else {
                        hid = true;
                    }
                    break;
                case 3:
                    if (str.getBytes().length > 640) {
                        mWatcher.setText("CustomInfo Exceed Max Length (640)");
                    } else {
                        hid = true;
                    }
                    break;
            }
            if (hid == true) {
                mWatcher.setVisibility(View.GONE);
            } else {
                mWatcher.setVisibility(View.VISIBLE);
            }
        }
    }

}
